package viewmodel;

import java.io.IOException;
import java.util.List;

import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.image.Image;
import org.zkoss.util.media.Media;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Messagebox.ClickEvent;

import util.FileUtils;

import admin.dao.UserDAO;

public class UserDataVM {
	
	private List<UserEditStatus> userEditData;
	private List<UserLevelModel> levelData;
	private boolean displayEdit = true;
	
	public UserDataVM() {
		userEditData = new UserDataModel().convertUserToEditData();
		levelData = UserLevelModel.createLevelList();
	}
	
	public boolean isDisplayEdit() {
		return displayEdit;
	}

	public List<UserEditStatus> getUserEditData() {
		return userEditData;
	}
	
	public List<UserLevelModel> getLevelData() {
		return levelData;
	}
	

	@Command
	//@NotifyChange("userEditData")
    public void delUser(@BindingParam("userEditStatus") UserEditStatus ues) {
		UserDAO.removeUser(ues.getUser());
  		userEditData.remove(ues);
		refreshRowTemplate(ues);
    }
	
	/**
	 * Đổi trạng thái: có edit <=> ko edit
	 */
	@Command
    public void changeEditableStatus(@BindingParam("userEditStatus") UserEditStatus ues) {
		ues.setEditingStatus(!ues.getEditingStatus());
        refreshRowTemplate(ues);
    }
	
	/**
	 * Hàm xử lý khi ấn nút "ok" 
	 */
	@Command
    public void confirm(@BindingParam("userEditStatus") UserEditStatus ues) {
		if(UserDAO.checNewUsername(ues.getUser().getUsername(),ues.getUser().getId())){			
			UserDAO.updateUser(ues);
		} else {
			Clients.showNotification("Username conflict.", "error", null, "middle_center", 1000);
		}
		changeEditableStatus(ues);
        refreshRowTemplate(ues);
    }
	
	@Command
    public void upload(@BindingParam("userEditStatus") UserEditStatus ues,@BindingParam("media") Media media) {
		try {
	        System.out.println(media.getName());
	        System.out.println(ues.getUser().getUsername());
	        
	        if (media instanceof Image) {
				String filePath = FileUtils.saveFileFromMedia(media, null,"member");
	        	ues.getUser().setAvatar(filePath);
	        	UserDAO.updateUser(ues);
	        } else {
	            //Messagebox.show("Not an image: "+media, "Error", Messagebox.OK, Messagebox.ERROR);
	        	Clients.showNotification("Not an image: "+media, "error", null, "middle_center", 1000);
	        }
		} catch (IOException e) {
			e.printStackTrace();
		}
		
        refreshRowTemplate(ues);
    }
	
	public void refreshRowTemplate(UserEditStatus userEditData) {
        /*
         * This code is special and notifies ZK that the bean's value
         * has changed as it is used in the template mechanism.
         * This stops the entire Grid's data from being refreshed
         */
        BindUtils.postNotifyChange(null, null, userEditData, "editingStatus");
    }
}
